;(defvar ring '(1 2 8 2 3 9 3 4 10 4 5 6 5 1 7))

; 10 9 1; 9 8 2
(defun make-perms(max)
  (permutations (loop for i from 1 to max collect i)))

(defun make-gon(l &optional (frst (car l)))
  (if (null (caddr l)) (cons (list (car l) frst (cadr l)) nil)
      (cons (list (car l) (caddr l) (cadr l)) (make-gon (cddr l) frst))))

(defun gonp(gon)
  (let ((sums (mapcar #'(lambda (x) (reduce #'+ x)) gon)))
    (loop for i in (cdr sums) when (/= (car sums) i) do (return nil) finally (return (car gon)))))

(defun concat-no(l)
  (if (null l) 0
      (+ (car l) (* (expt 10 (digits-no (car l))) (concat-no (cdr l))))))

(defun make-gon-no(gon)
  (concat-no (mapcar #'concat-no gon)))

(defun generate-gons(dim)
  (loop for i in (make-perms dim) 
     for gon = (make-gon i) 
     when (gonp gon) collect gon))

(defun probl68()
  (loop for i in 
       (remove-if #'
	(lambda (x) (>= x (expt 10 16)))
	(mapcar #'make-gon-no (generate-gons 10)))
       maximize i))

  
